﻿using MXComponent.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace MXComponent.DAL
{
    public partial class formulahistoryDB
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public formulahistoryDB()
        {
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">model</param>
        public void AddRecord(formulahistoryData model)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into formulahistory(");
            strSql.Append(@"formulahistoryid,formulaid,plancount,realcount,createtime,createby,isfinish) ");
            strSql.Append(" values (");
            strSql.Append(@"@formulahistoryid,@formulaid,@plancount,@realcount,@createtime,@createby,@isfinish)");
            SqlParameter[] parameters = {
                    new SqlParameter("@formulahistoryid", SqlDbType.NVarChar,50),
                    new SqlParameter("@formulaid", SqlDbType.Int),
                    new SqlParameter("@plancount", SqlDbType.Int),
                    new SqlParameter("@realcount", SqlDbType.Int),
                    new SqlParameter("@createtime", SqlDbType.DateTime),
                    new SqlParameter("@createby", SqlDbType.Int),
                    new SqlParameter("@isfinish", SqlDbType.Int)
                };
            parameters[0].Value = model.formulahistoryid;
            parameters[1].Value = model.formulaid;
            parameters[2].Value = model.plancount;
            parameters[3].Value = model.realcount;
            parameters[4].Value = model.createtime == string.Empty ? null : model.createtime;
            parameters[5].Value = model.createby;
            parameters[6].Value = 0;

            object ret = SqlHelper.ExecuteScalar(CommandType.Text, strSql.ToString(), parameters);
        }

        public void ClearTempDateTable()
        {
            SqlHelper.ExecteNonQuery(CommandType.Text, "delete from TempDateTable");
        }
        public void SaveTempDateTable(DataTable dt)
        {
            SqlHelper.BatchAdd(dt);
        }

        public void UpdateRecord(string formulahistoryid)
        {
            SqlHelper.ExecteNonQuery(CommandType.Text,string.Format( "update formulahistory set isfinish=1 where formulahistoryid='{0}'", formulahistoryid));
        }

        public void UpdateRecord(string formulahistoryid,int realcount)
        {
            SqlHelper.ExecteNonQuery(CommandType.Text, string.Format("update formulahistory set realcount={1} where formulahistoryid='{0}'", formulahistoryid, realcount));
        }

        public void UpdateRecordForPlancount(string formulahistoryid, int plancount)
        {
            SqlHelper.ExecteNonQuery(CommandType.Text, string.Format("update formulahistory set plancount={1} where formulahistoryid='{0}'", formulahistoryid, plancount));
        }

        public DataSet GetTopView()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select  Top 1 formulahistoryid,formulaid,plancount,realcount,isfinish from  formulahistory 
            ");
          
            strSql.Append(" order by createtime desc");
            return SqlHelper.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

    }
}